import { BaseWindow } from "../../../component/window/BaseWindow";
import { ObjCreate } from "../common/ObjCreate";
import { ResFileConst } from "../../resource/ResFileConst";
import { PreviewType } from "./PreviewType";
import { Language } from "../../../../extend/language/Language";
import { LoaderManager } from "../../../../com/gengine/resource/LoaderManager";
import { Cache } from "../../cache/Cache";
import { ItemConfig } from "../../resource/ItemConfig";
import { ChangeHeadIconConfig } from "../../resource/ChangeHeadIconConfig";
import { ChangeHeadIconInfo } from "../../resource/info/ChangeHeadIconInfo";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { ESex } from "../../../../Message/Public/ESex";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { ImagesConst } from "../../resource/ImagesConst";
import { AvatarInfo } from "../../resource/info/AvatarInfo";
import { AvatarType } from "../../resource/info/AvatarType";
import { UserPlayer } from "../../scene/player/entity/UserPlayer";
import { RolePlayer } from "../../scene/player/entity/RolePlayer";
import { DirectionType } from "../../scene/modle/data/DirectionType";
import { SWFPlayer } from "../../scene/modle/SWFPlayer";
import { PetConfig } from "../../resource/PetConfig";
import { ModelUtil } from "../../scene/player/info/ModelUtil";
import { ModelType } from "../../scene/modle/data/ModelType";
import { ImageInfo } from "../../../../com/gengine/resource/info/ImageInfo";
import { SWFInfo } from "../../../../com/gengine/resource/info/SWFInfo";
import { GLoadedButton } from "../../../../com/mui/controls/GLoadedButton";
type int = number;
//class PreviewWindow
    
    export  class PreviewWindow extends BaseWindow
    {
        constructor()
        {
            super();
            setSize(238, 269);
            titleHeight = 30;
            return;
        }

        protected /* override */ createChildren(): void
        {
            super.createChildren();
            this._bgSprite = new Object /* flash.display.Sprite */();
            this.addChild(this._bgSprite);
            this._bg = new Object /* flash.display.Bitmap */();
            this._bgSprite.addChild(this._bg);
            this._preButton = ObjCreate.createGLoadedButton(ResFileConst.Left, 15, 220, 49, 26, this);
            this._preButton.visible = false;
            this._preButton.addEventListener(flash.events.MouseEvent.CLICK, this.onPreButtonClickHandler);
            this._nextButton = ObjCreate.createGLoadedButton(ResFileConst.Right, 175, 220, 49, 26, this);
            this._nextButton.visible = false;
            this._nextButton.addEventListener(flash.events.MouseEvent.CLICK, this.onNextButtonClickHandler);
            this._avatarTypeBm = new Object /* flash.display.Bitmap */();
            this._avatarSexBm = new Object /* flash.display.Bitmap */();
            this._avatarBm = new Object /* flash.display.Bitmap */();
            return;
        }

        public updatePetModel(arg1: int, arg2: int=1): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc5=0;
            var loc6=null;
            var loc7=0;
            this._type = arg2;
            if (arg2 != PreviewType.PreviewPet) 
            {
                if (arg2 != PreviewType.PreviewMount) 
                {
                    if (arg2 == PreviewType.PreviewAvatar) 
                    {
                        this._preButton.visible = false;
                        this._nextButton.visible = false;
                        this._bgSprite.x = 18;
                        this._bgSprite.y = 39;
                        title = Language.getString(70512);
                        LoaderManager.instance.load("PreviewAvatar.swf", this.onBgPicLoaded);
                        loc3 = Cache.instance.role;
                        if ((loc4 = ItemConfig.instance.getInfoByCode(arg1)).item.codeUnbind > 0) 
                        {
                            loc5 = loc4.item.codeUnbind;
                        }
                        else 
                        {
                            loc5 = arg1;
                        }
                        loc7 = (loc6 = ChangeHeadIconConfig.instance.getHeadIconId(loc5) as ChangeHeadIconInfo).avatarid;
                        this._avatarSex = loc6.sex;
                        this._avatarType = loc6.type;
                        LoaderHelp.addResCallBack(ResFileConst.ChangeHeadIcon, this.onLoadBmResource);
                        if (this._avatarSex != ESex._ESexMan) 
                        {
                            this._avatarSexBm.bitmapData = GlobalClass.getBitmapData(ImagesConst.GirlIcon_upSkin);
                        }
                        else 
                        {
                            this._avatarSexBm.bitmapData = GlobalClass.getBitmapData(ImagesConst.BoyIcon_upSkin);
                        }
                        this._avatarSexBm.x = 154;
                        this._avatarSexBm.y = 42;
                        if (this.contains(this._avatarSexBm) == false) 
                        {
                            this.addChild(this._avatarSexBm);
                        }
                        AvatarInfo.getAvatar(loc3.entityInfo.camp, this._avatarSex, AvatarType.BIG, this.onAvatarLoaded, loc7);
                        this._avatarBm.x = 87;
                        this._avatarBm.y = 122;
                        if (this.contains(this._avatarBm) == false) 
                        {
                            this.addChild(this._avatarBm);
                        }
                    }
                }
                else 
                {
                    this._preButton.visible = true;
                    this._nextButton.visible = true;
                    this._bgSprite.x = 10;
                    this._bgSprite.y = 39;
                    title = Language.getString(53019);
                    LoaderManager.instance.load("MountPreviewBg.swf", this.onBgPicLoaded);
                    if (this._userPlayer == null) 
                    {
                        this._userPlayer = new UserPlayer();
                    }
                    this._userPlayer.play();
                    this._userPlayer.camp = RolePlayer.instance.camp;
                    this._userPlayer.sex = RolePlayer.instance.sex;
                    this._userPlayer.career = RolePlayer.instance.career;
                    this._userPlayer.updateClothes(RolePlayer.instance.clothesID);
                    this._userPlayer.updateMount(arg1);
                    this._userPlayer.direction = DirectionType.SouthWest;
                    this._userPlayer.x = 120;
                    this._userPlayer.y = 200;
                    if (this.contains(this._userPlayer) == false) 
                    {
                        this.addChild(this._userPlayer);
                    }
                }
            }
            else 
            {
                this._preButton.visible = true;
                this._nextButton.visible = true;
                this._bgSprite.x = 10;
                this._bgSprite.y = 39;
                title = Language.getString(53018);
                LoaderManager.instance.load("PetPreviewBg.swf", this.onBgPicLoaded);
                if (this._modellayer == null) 
                {
                    this._modellayer = new SWFPlayer();
                }
                if (loc2 = PetConfig.instance.getItemInfoByCode(arg1)) 
                {
                    this._modellayer.load(ModelUtil.getModelSWF(loc2.pet.modelId), ModelType.Monster, loc2);
                }
                this._modellayer.move(115, 217);
                if (this.contains(this._modellayer) == false) 
                {
                    this.addChild(this._modellayer);
                }
                this.onLoadCompleteHandler(null);
            }
            return;
        }

        /* internal  */onLoadBmResource(): void
        {
            if (this._avatarType != 1) 
            {
                if (this._avatarType != 2) 
                {
                    this._avatarTypeBm.bitmapData = GlobalClass.getBitmapData(ImagesConst.SbLoverHeadIcon);
                }
                else 
                {
                    this._avatarTypeBm.bitmapData = GlobalClass.getBitmapData(ImagesConst.SbSpecialHeadIcon);
                }
            }
            else 
            {
                this._avatarTypeBm.bitmapData = GlobalClass.getBitmapData(ImagesConst.SbNormalHeadIcon);
            }
            this._avatarTypeBm.x = 15;
            this._avatarTypeBm.y = 40;
            if (this.contains(this._avatarTypeBm) == false) 
            {
                this.addChild(this._avatarTypeBm);
            }
            return;
        }

        /* internal  */onAvatarLoaded(arg1: ImageInfo): void
        {
            this._avatarBm.bitmapData = arg1.bitmapData;
            return;
        }

        /* internal  */onNextButtonClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._type != PreviewType.PreviewPet) 
            {
                if (this._type == PreviewType.PreviewMount) 
                {
                    if (this._userPlayer.direction > 1) 
                    {
                        if (this._userPlayer.direction != 8) 
                        {
                            if (this._userPlayer.direction != 4) 
                            {
                                --this._userPlayer.direction;
                            }
                            else 
                            {
                                this._userPlayer.direction = 2;
                            }
                        }
                        else 
                        {
                            this._userPlayer.direction = 6;
                        }
                    }
                    else 
                    {
                        this._userPlayer.direction = 8;
                    }
                }
            }
            else 
            {
                if (this._modellayer.currentDirection > 1) 
                {
                    --this._modellayer.currentDirection;
                }
                else 
                {
                    this._modellayer.currentDirection = 8;
                }
                this._modellayer.updateFrame(this._modellayer.currentAction, this._modellayer.currentDirection);
            }
            return;
        }

        /* internal  */onPreButtonClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._type != PreviewType.PreviewPet) 
            {
                if (this._type == PreviewType.PreviewMount) 
                {
                    if (this._userPlayer.direction < 8) 
                    {
                        if (this._userPlayer.direction != 2) 
                        {
                            if (this._userPlayer.direction != 6) 
                            {
                                this._userPlayer.direction = this._userPlayer.direction + 1;
                            }
                            else 
                            {
                                this._userPlayer.direction = 8;
                            }
                        }
                        else 
                        {
                            this._userPlayer.direction = 4;
                        }
                    }
                    else 
                    {
                        this._userPlayer.direction = 1;
                    }
                }
            }
            else 
            {
                if (this._modellayer.currentDirection < 8) 
                {
                    this._modellayer.currentDirection = this._modellayer.currentDirection + 1;
                }
                else 
                {
                    this._modellayer.currentDirection = 1;
                }
                this._modellayer.updateFrame(this._modellayer.currentAction, this._modellayer.currentDirection);
            }
            return;
        }

        /* internal  */onLoadCompleteHandler(arg1: SWFPlayer): void
        {
            if (this._modellayer.height > 150) 
            {
                this._modellayer.sceneY = 235;
            }
            else 
            {
                this._modellayer.sceneY = 215;
            }
            this._modellayer.updateCurrentFrame(0);
            return;
        }

        /* internal  */onCloseButtonClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this.hide();
            return;
        }

        public /* override */ hide(): void
        {
            super.hide();
            this.clear();
            return;
        }

        /* internal  */onBgPicLoaded(arg1: SWFInfo): void
        {
            this._bg.bitmapData = arg1.bitmapData;
            return;
        }

        public clear(): void
        {
            if (this._modellayer) 
            {
                if (this.contains(this._modellayer)) 
                {
                    this.removeChild(this._modellayer);
                    this._modellayer.dispose();
                }
            }
            if (this._userPlayer) 
            {
                if (this.contains(this._userPlayer)) 
                {
                    this.removeChild(this._userPlayer);
                }
            }
            if (this._avatarBm) 
            {
                this._avatarBm.bitmapData = null;
            }
            if (this._avatarTypeBm) 
            {
                this._avatarTypeBm.bitmapData = null;
            }
            if (this._avatarSexBm) 
            {
                this._avatarSexBm.bitmapData = null;
            }
            return;
        }

        /* internal */ /* var */_bgSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_centerSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_bg: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_bgPlayer: SWFPlayer;

        /* internal */ /* var */_modellayer: SWFPlayer;

        /* internal */ /* var */_userPlayer: UserPlayer;

        /* internal */ /* var */_preButton: GLoadedButton;

        /* internal */ /* var */_nextButton: GLoadedButton;

        /* internal */ /* var */_avatarBm: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_avatarTypeBm: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_avatarSexBm: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_avatarSex: int;

        /* internal */ /* var */_avatarType: int;

        /* internal */ /* var */_type: int;
    }
